/*
    OpenSys by Alexander Ljubizki
    Started 12.08.2013
    For more Info read the ChangeLog.txt
*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/* Interne */
#include "System/Configuration.h"
#include "Graphic/Graphic.h"
#include "Graphic/Input.h"
#include "Menu/Menu.h"
#include "Menu/Logic.h"
#include "Entity/Entity.h"
#include "Entity/Player.h"
#include "Log/Log.h"

/* Funktionen */
static void Quit(int rc);

int Runnig;

int main_process() {
    /* Menü rechnen */
    Menu_Process();
    /* Entity */
    Entity_Process();
    /* Player */
    Player_Input();
    /* Auf eingabe reagieren */
    Input_Process();
    return 5;
}

void main_Quit() {
    Runnig = 0;
}

/* Hauptfunktion */
int main(int argc, char *argv[]) {
    int i;
    SDL_TimerID Timer;

    /* Zufall */
    srand(time(0));

    /* Config */
    Configuration_Init();

    /* Log einstellen */
    logInit();

    /* Graphic starten */
    if (!Graphic_Init()) {
        logError("Graphics can't be initialized!");
        return -1;
    }

    /* Input */
    Input_Init();

    /* Entiy */
    Entity_Init();
    Entity_Player_Init();

    /* World */
    World_Init();

    /* Menu Logic */
    Logic_Init();

    /* Programm startet */
    Runnig = 1;

    /* Timer */
    Timer = SDL_AddTimer( 50, main_process, NULL);

    /* Config schließen */
    Configuration_Quit();

    World_Load(0);
    Entity_Create( 10, 550, 100, 1);
    Entity_Create( 10, 500, 100, 1);
    Player_Connect();

    /* Hauptschleife */
    while(Runnig) {
        /* Zeichne die karte */
        World_Graphic();
        /* Zeichne die Menü's */
        Menu_Graphic();
        /* Entity zeichnen */
        Entity_Process_Graphic();
        /* Grafik darstellen */
        if(!Graphic_Process())
            Runnig = 0;
    }

    /* Graphic beenden */
    Graphic_Quit();

    /* Programm beenden */
    SDL_RemoveTimer( Timer);
    Quit(0);

    return 0;
}

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void Quit(int rc) {
    exit(rc);
}
